package com.layui.ad.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.layui.ad.annotation.DataSource;
import com.layui.ad.entity.Level;
import com.layui.ad.enums.DataSourceEnum;
import com.layui.ad.mapper.LevelMapper;
import com.layui.ad.query.LevelQuery;
import com.layui.ad.service.LevelService;
import com.layui.ad.utils.*;
import com.layui.ad.vo.LevelListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class LevelServiceImpl extends ServiceImpl<LevelMapper, Level> implements LevelService {

    @Autowired
    private LevelMapper levelMapper;

    @Override
    @DataSource(DataSourceEnum.SYSSOURCE)
    public JsonResult getList(LevelQuery levelQuery) {
        QueryWrapper<Level> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(levelQuery.getName())) {
            queryWrapper.like("name", levelQuery.getName());
        }

        if (levelQuery.getStatus() != null && levelQuery.getStatus() > 0) {
            queryWrapper.eq("status", levelQuery.getStatus());
        }

        IPage<Level> page = new Page<>(levelQuery.getPage(), levelQuery.getLimit());
        IPage<Level> data = levelMapper.selectPage(page, queryWrapper);
        List<Level> levelList = data.getRecords();
        List<LevelListVo> levelListVoList = new ArrayList<>();

        if (!levelList.isEmpty()) {
            levelList.forEach(item -> {
                LevelListVo levelListVo = new LevelListVo();
                BeanUtils.copyProperties(item, levelListVo);
                if (item.getStatus().equals(1)) {
                    levelListVo.setStatusName("正常");
                } else {
                    levelListVo.setStatusName("异常");
                }
                levelListVo.setUpdateUserName(AdminUtils.getAdminName(item.getUpdateUser()));
                levelListVo.setCreateUserName(AdminUtils.getAdminName(item.getCreateUser()));
                levelListVoList.add(levelListVo);
            });
        }

        return JsonResult.ok("操作成功", levelListVoList, data.getTotal());
    }

    @Override
    @DataSource(DataSourceEnum.SYSSOURCE)
    public Object getInfo(Integer id) {
        Object level = levelMapper.selectById(id);
        return level;
    }

    @Override
    @DataSource(DataSourceEnum.SYSSOURCE)
    public JsonResult edit(Level level) {
        Integer num = 0;
        if (level.getId() != null && level.getId() > 0) {
            level.setUpdateUser(ShiroUtils.getAdminId());
            level.setUpdateTime(DateUtils.now());
            num = levelMapper.updateById(level);
        } else {
            level.setCreateTime(DateUtils.now());
            level.setCreateUser(ShiroUtils.getAdminId());
            num = levelMapper.insert(level);
        }
        if (num.equals(1)) {
            return JsonResult.ok();
        } else {
            return JsonResult.fail();
        }

    }

    @Override
    @DataSource(DataSourceEnum.SYSSOURCE)
    public JsonResult delete(Integer id) {
        if (id == null && id <= 0) {
            return JsonResult.fail("缺少参数或参数异常");
        }
        Object level = levelMapper.selectById(id);
        if (level == null) {
            return JsonResult.fail("数据不存在");
        }
        Integer num = levelMapper.deleteById(id);
        if (num > 0) {
            return JsonResult.ok();
        } else {
            return JsonResult.fail();
        }
    }


    @Override
    @DataSource(DataSourceEnum.SYSSOURCE)
    public JsonResult batchDelete(String ids) {
        String[] idsList = ids.split(",");
        List<String> idList = Arrays.asList(idsList);
        Integer num = levelMapper.deleteBatchIds(idList);
        if (num > 0) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @Override
    @DataSource(DataSourceEnum.SYSSOURCE)
    public JsonResult batchStatus(Level entiry) {
        if (StringUtils.isEmpty(entiry.getIds())) {
            return JsonResult.fail("请选择数据");
        }
        String[] ids = entiry.getIds().split(",");
        Integer totalNum = 0;
        for (String s : ids) {
            Level level = new Level();
            level.setId(Integer.valueOf(s));
            level.setStatus(entiry.getStatus());
            Integer result = levelMapper.updateById(level);
            if (result > 0) {
                totalNum++;
            }
        }
        if (totalNum != ids.length) {
            return JsonResult.fail("批量修改状态失败");
        }
        return JsonResult.ok();
    }

    @Override
    @DataSource(DataSourceEnum.SYSSOURCE)
    public JsonResult setStatus(Level levelDto) {
        if (levelDto.getId() == null || levelDto.getId() == 0) {
            return JsonResult.fail("请选择要操作的数据");
        }
        Integer result = levelMapper.updateById(levelDto);
        if (result > 0) {
            return JsonResult.ok();
        } else {
            return JsonResult.fail();
        }
    }
}
